package pckDao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import pckEntidad.Color;
import pckUtil.Conexion;


public class ColorDao  {
  
    private Conexion oConexion=new Conexion();
    private Color oColor = null;
    private List<Color> lisColor=null;
    
    public void insertar(Color oCol){
         String sql = "{call sping_Color(?,?,?,?)}";
         CallableStatement cst;
        try {
            cst = oConexion.getConnection().prepareCall(sql);
            cst.setString(1, oCol.getNombcol());
            cst.setString(2, oCol.getDesccol());
            cst.setString(3, oCol.getCodcolo());
            cst.setString(4, oCol.getMarca().getIdmarc());
            cst.execute();            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }   
     }
    
    
    public List<Color> listar(){
    String sql = "{call splis_Color}";
        try {
            CallableStatement cst = oConexion.getConnection().prepareCall(sql);
            ResultSet rst = cst.executeQuery();
            lisColor= new ArrayList<Color>();
            while(rst.next()){
                oColor = new Color();
                oColor.setIdcolo(rst.getString(1));  
                oColor.setNombcol(rst.getString(2));
                oColor.setDesccol(rst.getString(3));
                oColor.setMarca(new MarcaDao().obtener(rst.getString(4)));
                lisColor.add(oColor);
            }
            return  lisColor;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
         return null;
     }
}
